<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<section class="flex flex-col" [formGroup]="keyFilterListFormGroup">
  <mat-accordion [multi]="true">
    <mat-expansion-panel [expanded]="true">
      <mat-expansion-panel-header>
        <mat-panel-title>
          <div translate>filter.key-filters</div>
        </mat-panel-title>
      </mat-expansion-panel-header>
      <div class="flex flex-row">
        <span class="max-w-8% flex-full"></span>
        <div class="flex max-w-92% flex-full flex-row items-center justify-start">
          <label translate class="tb-title no-padding flex-1">filter.key-name</label>
          <label translate class="tb-title no-padding flex-1">filter.key-type.key-type</label>
          <span [class.!hidden]="disabled" style="min-width: 80px;">&nbsp;</span>
          <span [class.!hidden]="!disabled" style="min-width: 40px;">&nbsp;</span>
        </div>
      </div>
      <mat-divider></mat-divider>
      <div class="key-filter-list">
        <div class="flex flex-row items-stretch justify-start" style="max-height: 76px;"
             formArrayName="keyFilters"
             *ngFor="let keyFilterControl of keyFiltersFormArray.controls; let $index = index">
          <div class="filters-operation max-w-8% flex-full">
            <span *ngIf="$index > 0" translate>filter.operation.and</span>
          </div>
          <div class="flex max-w-92% flex-full flex-col">
            <div class="flex flex-row items-center justify-start">
              <div class="flex-1">{{ keyFilterControl.value.key.key }}</div>
              <div class="flex-1">{{ entityKeyTypeTranslations.get(keyFilterControl.value.key.type) | translate }}</div>
              <button mat-icon-button color="primary"
                      type="button"
                      (click)="editKeyFilter($index)"
                      matTooltip="{{ (disabled ? 'filter.key-filter' : 'filter.edit-key-filter') | translate }}"
                      matTooltipPosition="above">
                <mat-icon>{{disabled ? 'more_vert' : 'edit'}}</mat-icon>
              </button>
              <button mat-icon-button color="primary"
                      [class.!hidden]="disabled"
                      type="button"
                      (click)="removeKeyFilter($index)"
                      matTooltip="{{ 'filter.remove-key-filter' | translate }}"
                      matTooltipPosition="above">
                <mat-icon>close</mat-icon>
              </button>
            </div>
            <mat-divider></mat-divider>
          </div>
        </div>
        <span [class.!hidden]="!!keyFiltersFormArray.length"
              [class.disabled]="disabled"
              class="no-data-found flex items-center justify-center" translate>filter.no-key-filters</span>
      </div>
      <div style="margin-top: 16px;">
        <button mat-button mat-raised-button color="primary"
                [class.!hidden]="disabled"
                (click)="addKeyFilter()"
                type="button"
                matTooltip="{{ 'filter.add-key-filter' | translate }}"
                matTooltipPosition="above">
          {{ 'filter.add-key-filter' | translate }}
        </button>
      </div>
    </mat-expansion-panel>
    <mat-expansion-panel [expanded]="true">
      <mat-expansion-panel-header>
        <mat-panel-title>
          <div translate>filter.preview</div>
        </mat-panel-title>
      </mat-expansion-panel-header>
      <div class="tb-filter-preview">
        <tb-filter-text [formControl]="keyFiltersControl"></tb-filter-text>
      </div>
    </mat-expansion-panel>
  </mat-accordion>
</section>
